import { createWebHashHistory, createRouter } from 'vue-router'
import Layout from '/@/layout/Index.vue'
import store from '/@/store/Index'
const history = createWebHashHistory()
const router = createRouter({
  history,
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import(/* webpackChunkName: 'login' */ '/@/views/login/Index.vue')
    },
    {
      path: '/',
      component: Layout,
      meta: {
        requiresAuth: true
      },
      children: [
        {
          path: '', // 默认子路由
          name: 'home',
          component: () => import(/* webpackChunkName: 'home' */ '/@/views/home/Index.vue')
        },
        {
          path: '/role',
          name: 'role',
          component: () => import(/* webpackChunkName: 'role' */ '/@/views/role/Index.vue')
        },
        {
          path: '/menu',
          name: 'menu',
          component: () => import(/* webpackChunkName: 'menu' */ '/@/views/menu/Index.vue')
        },
        {
          path: '/resource',
          name: 'resource',
          component: () => import(/* webpackChunkName: 'resource' */ '/@/views/resource/Index.vue')
        },
        {
          path: '/course',
          name: 'course',
          component: () => import(/* webpackChunkName: 'course' */ '/@/views/course/Index.vue')
        },
        {
          path: '/course/create',
          name: 'course-create',
          props: true,
          component: () =>
            import(/* webpackChunkName: 'course-create' */ '/@/views/course/Create.vue')
        },
        {
          path: '/course/:courseId/edit',
          name: 'course-edit',
          props: true,
          component: () => import(/* webpackChunkName: 'course-edit' */ '/@/views/course/Edit.vue')
        },
        {
          path: '/course/:courseId/section',
          name: 'course-section',
          props: true,
          component: () =>
            import(/* webpackChunkName: 'course-section' */ '/@/views/course/Section.vue')
        },
        {
          path: '/user',
          name: 'user',
          component: () => import(/* webpackChunkName: 'user' */ '/@/views/user/Index.vue')
        },
        {
          path: '/advert',
          name: 'advert',
          component: () => import(/* webpackChunkName: 'advert' */ '/@/views/advert/Index.vue')
        },
        {
          path: '/advert-space',
          name: 'advert-space',
          component: () =>
            import(/* webpackChunkName: 'advert-space' */ '/@/views/advert-space/Index.vue')
        },
        {
          path: '/menu/create',
          name: 'menu-create',
          component: () =>
            import(/* webpackChunkName: 'menu-create-edit' */ '/@/views/menu/Create.vue')
        },
        {
          path: '/menu/:id/edit',
          name: 'menu-edit',
          component: () =>
            import(/* webpackChunkName: 'menu-create-edit' */ '/@/views/menu/Edit.vue')
        },
        {
          path: '/role/:roleId/alloc-menu',
          name: 'alloc-menu',
          props: true,
          component: () =>
            import(/* webpackChunkName: 'alloc-menu' */ '/@/views/role/AllocMenu.vue')
        },
        {
          path: '/course/:courseId/video',
          name: 'course-video',
          props: true,
          component: () => import(/* webpackChunkName: 'course-video' */ '@/views/course/video.vue')
        }
      ]
    },
    {
      path: '/:pathMatch(.*)',
      name: '404',
      component: () => import(/* webpackChunkName: '404' */ '/@/views/error-page/404.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!store.state.user) {
      next({
        name: 'login',
        query: {
          // 通过url传递查询字符串参数
          redirect: to.fullPath
        }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
